package com.baichang.taoshunda.service.impl;


import com.alipay.util.AlipayUtil;
import com.baichang.framework.utils.MathUtils;
import com.baichang.taoshunda.dao.UserRechargeHistoryDao;
import com.baichang.taoshunda.entity.UserRechargeHistory;
import com.baichang.taoshunda.service.ServerService;
import com.baichang.taoshunda.service.UserRechargeHistoryService;

import com.baichang.taoshunda.weixin.tencent.WeixinUtils;
import com.qianmi.open.api.qmcs.QmcsClient;
import com.qianmi.open.api.qmcs.channel.ChannelException;
import com.qianmi.open.api.qmcs.handler.MessageHandler;
import com.qianmi.open.api.qmcs.message.Message;
import com.qianmi.open.api.qmcs.message.MessageStatus;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * @param
 * @author: wsl
 * @Date: 2019/1/23 17：48
 * @Description:
 */
@Service
public class ServerServiceImpl implements ServerService {
    @Autowired
    private UserRechargeHistoryDao userRechargeHistoryDao;
    @Autowired
    private UserRechargeHistoryService userRechargeHistoryService;
    protected static String url = "http://api.bm001.com/api";
    protected static String appKey = "10001319";
    protected static String appSecret = "gUjpChpSnyrEr4RdURxtyx695kLE70Ar";

    @Override
    public boolean start() {


        System.out.println("进入==============》");
        //创建链接客户端,使用默认分组
        QmcsClient client = new QmcsClient(appKey, appSecret, "A1795483");
        //消息处理
        client.setMessageHandler(new MessageHandler() {
            @Override
            public void onMessage(Message message, MessageStatus status) throws Exception {
                System.out.println("建立分组成功");
                try {
                    JSONObject messageObject = JSONObject.fromObject(message.getContent());
                    System.out.println(messageObject.toString());
                    System.out.println("ssssss" + messageObject.get("outer_tid").toString());
                    UserRechargeHistory history = userRechargeHistoryDao.getHistoryByOrderNumber(messageObject.get("outer_tid").toString());
                    System.out.println("获取历史：" + history);
                    if ("1".equals(messageObject.get("recharge_state").toString())) {
                        userRechargeHistoryService.updateStatus(1, history.getPaymentUnit(), history.getId());
                    } else {
                        if (history.getPayMethod() == 1) {
                            AlipayUtil.refundMoney(history.getPayOrderKid(), history.getPreferentialAmount().doubleValue());
                        } else {
                            WeixinUtils.payMallRefund(history.getPayOrderKid(), (int) MathUtils.mul(history.getPreferentialAmount().doubleValue(), 100D), (int) MathUtils.mul(history.getPreferentialAmount().doubleValue(), 100D));
                        }
                    }
                    //以下仅为示例代码,实际业务处理根据自身需要开发
                    System.out.println(message.getTopic());
                    System.out.println(message.getContent());
                } catch (Exception e) {
                    e.printStackTrace();
                    //消息处理失败,需要服务端重新推送
                    status.fail();
                }
            }
        });
        try {
            //发起连接请求
            client.connect("ws://mc.bm001.com");
//            try {
//                System.in.read();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        } catch (ChannelException e) {
            e.printStackTrace();
        }
        return false;
    }


}
